<template>
    <div style="height:100%;width:100%;position:relative;">
        <div>
            <div class="pagetop">
                <div class="top-row">
                    <span class="top-row-title">报告单元</span>
                    <span class="top-row-item2">
                        <rm-ddl-rptunit prgno="LIS253" v-model="searchParam.rptunitid" width="100%" @change="rptunitchange"></rm-ddl-rptunit>
                    </span>
                    <span class="top-row-title">病历号</span>
                    <span class="top-row-item">
                        <el-input v-model="searchParam.pat_no" clearable></el-input>
                    </span>
                    <span class="top-row-title">姓名</span>
                    <span class="top-row-item">
                        <el-input v-model.trim="searchParam.pat_name" @keyup.enter.native="queryRequncharge" clearable></el-input>
                    </span>
                    <span class="top-row-title">床号</span>
                    <span class="top-row-item">
                        <el-input v-model.trim="searchParam.req_bedno" @keyup.enter.native="queryRequncharge" clearable></el-input>
                    </span>
                    <span class="top-row-title">计价标志</span>
                    <span style="width: 80px;margin-right: 5px;">
                        <el-select v-model="searchParam.charge_flag" placeholder="请选择" style="width:100%;">
                            <el-option label="全部" value=""></el-option>
                            <el-option label="已计价" value="1"></el-option>
                            <el-option label="未计价" value="0"></el-option>
                            <el-option label="待退费" value="8"></el-option>
                            <el-option label="已退费" value="9"></el-option>
                        </el-select>
                    </span>
                </div>
                <div class="top-row">
                    <span class="top-row-title">标本日期</span>
                    <span>
                        <el-date-picker v-model="searchParam.datetime1" type="date" value-format="yyyy-MM-dd" placeholder="开始日期" :clearable="false" style="width:110px">
                        </el-date-picker> 至
                        <el-date-picker v-model="searchParam.datetime2" type="date" value-format="yyyy-MM-dd" placeholder="结束日期" :clearable="false" style="width:110px">
                        </el-date-picker>
                    </span>
                    <span class="top-row-title">条码号</span>
                    <span class="top-row-item">
                        <el-input v-model="searchParam.barcode" clearable></el-input>
                    </span>
                    <span class="top-row-title">病人类型</span>
                    <span class="top-row-item">
                        <rm-ddl-common datatype="seccode" kindno="PT" dataname="病人类型" v-model="searchParam.pat_typecode" :clearable="true" @change="pattypechange" width="130"></rm-ddl-common>
                    </span>
                    <span class="top-row-title">退费原因</span>
                    <span class="top-row-item">
                        <el-input v-model="searchParam.uncharge_reason" clearable></el-input>
                    </span>
                    <span style="width: 80px;margin: 0 5px 0 18px;">
                        <el-checkbox v-model="searchParam.rptunitidflag" true-label="1" false-label="0"></el-checkbox> 无对应报告
                    </span>
                </div>
                <div class="top-button">
                    <el-button icon="el-icon-search" v-has="'PINQUERY'" type="primary" @click="queryRequncharge">查询</el-button>
                    <el-button icon="el-icon-edit-outline" @click="BeforeBillingAndRefund(1)">计费</el-button>
                    <el-button icon="el-icon-remove-outline" @click="BeforeBillingAndRefund(2)">退费</el-button>
                    <!-- <el-button icon="el-icon-printer" @click="printRequncharge">打印</el-button> -->
                    <el-button icon="el-icon-upload2" @click="exportExcel">导出</el-button>
                    <!-- <el-button @click="setExportColumn">导出列设置</el-button> -->
                    <el-button icon="el-icon-delete" @click="clearSearchParam">清除</el-button>
                </div>
            </div>
            <div class="pagecenter">
                <div class="middle-center" ref="xTableDiv" style="height:100%;">
                    <div style="height:100%">
                        <vxe-table ref="xTable" :data="tableList" :loading="tableLoading" border height="auto" header-align="center" resizable 
                            :keyboard-local-config="keyLocalConfig" show-overflow="title" size="mini" stripe highlight-current-row 
                            :sort-config="{trigger: 'cell'}" :edit-config="{trigger: 'click', mode: 'cell'}">
                            <vxe-table-column type="selection" width="45" align="center"></vxe-table-column>
                            <vxe-table-column field="pat_typename" sortable title="病人来源" width="60"></vxe-table-column>
                            <vxe-table-column field="pat_no" sortable title="病历号" width="80"></vxe-table-column>
                            <vxe-table-column field="pat_name" sortable title="姓名" width="80"></vxe-table-column>
                            <vxe-table-column field="req_groupna" title="分单类别" width="80"></vxe-table-column>
                            <vxe-table-column field="barcode" sortable title="条码号" width="100"></vxe-table-column>
                            <vxe-table-column field="recieve_dt" title="接收时间" width="120"></vxe-table-column>
                            <vxe-table-column field="recieve_username" title="接收人" width="60"></vxe-table-column>
                            <vxe-table-column field="charge_flag" title="计价标志" width="60" :formatter="formatChargeflag"></vxe-table-column>
                            <vxe-table-column field="req_itemname" title="申请项目名称" width="160"></vxe-table-column>
                            <vxe-table-column field="item_price" title="单价" width="40"></vxe-table-column>
                            <vxe-table-column field="qty" title="数量" width="40"></vxe-table-column>
                            <vxe-table-column field="emer_flag" title="加急" width="40" align="center">
                                <template v-slot="{row}">
                                    <span style="color:red">{{row.emer_flag=="1"?"急":""}}</span>
                                </template>
                            </vxe-table-column>
                            <vxe-table-column field="req_statname" title="申请状态" width="60"></vxe-table-column>
                            <vxe-table-column field="req_deptname" title="申请科室" width="94"></vxe-table-column>
                            <vxe-table-column field="req_docname" title="申请医生" width="60"></vxe-table-column>
                            <vxe-table-column field="req_bedno" title="床号" width="40" align="center"></vxe-table-column>
                            <vxe-table-column field="specimen_name" title="标本类型" width="80"></vxe-table-column>
                            <vxe-table-column field="pat_sexname" title="性别" width="40" align="center"></vxe-table-column>
                            <vxe-table-column field="labadd" title="实验室增加标志" width="65" align="center">
                                <template v-slot="{row}">
                                    <el-checkbox v-model="row.labadd" true-label="1" false-label="0" disabled></el-checkbox>
                                </template>
                            </vxe-table-column>
                            <vxe-table-column field="create_username" title="创建用户" width="60"></vxe-table-column>
                            <vxe-table-column field="report_id" title="报告单ID" width="100"></vxe-table-column>
                            <vxe-table-column field="charge_typename" title="收费类型" width="60"></vxe-table-column>
                            <vxe-table-column field="req_dt" title="申请日期" width="120"></vxe-table-column>
                            <vxe-table-column field="create_dt" title="创建时间" width="120"></vxe-table-column>
                            <vxe-table-column field="pat_id" title="病人ID号" width="110"></vxe-table-column>
                            <vxe-table-column field="inp_date" title="入院日期" width="120"></vxe-table-column>
                            <vxe-table-column field="test_dt" title="上机时间" width="120"></vxe-table-column>
                            <vxe-table-column field="test_username" title="上机用户 " width="60"></vxe-table-column>
                            <vxe-table-column field="report_dt" title="报告时间" width="120"></vxe-table-column>
                            <vxe-table-column field="report_username" title="检验医生姓名" width="60"></vxe-table-column>
                            <vxe-table-column field="rechk_username" title="核对医生姓名" width="60"></vxe-table-column>
                            <vxe-table-column field="saleman" title="销售员" width="60"></vxe-table-column>
                            <vxe-table-column field="pat_cardno" title="就诊卡号" width="100"></vxe-table-column>
                            <vxe-table-column field="inp_id" title="住院标识/就诊号" width="60"></vxe-table-column>
                            <vxe-table-column field="perform_deptname" title="执行科室" width="80"></vxe-table-column>
                            <vxe-table-column field="req_wardno" title="病区代码" width="60"></vxe-table-column>
                            <vxe-table-column field="original_reqno" title="原始单号" width="80"></vxe-table-column>
                            <vxe-table-column field="sample_detail" title="标本备注说明 " width="121"></vxe-table-column>
                            <vxe-table-column field="reqtype" title="申请单类别" width="50"></vxe-table-column>
                            <vxe-table-column field="reqsource" title="申请单来源" width="50"></vxe-table-column>
                            <vxe-table-column field="seq" title="序号" width="40" align="center"></vxe-table-column>
                            <vxe-table-column field="req_itemcode" title="申请项目代码" width="100"></vxe-table-column>
                            <vxe-table-column field="his_itemcode" title="申请项目HIS代码" width="100"></vxe-table-column>
                            <vxe-table-column field="his_recordid" title="HIS_RecordID" width="90"></vxe-table-column>
                            <vxe-table-column field="his_refcol1" title="HIS_RefCol1" width="80"></vxe-table-column>
                            <vxe-table-column field="his_refcol2" title="HIS_RefCol2" width="80"></vxe-table-column>
                            <vxe-table-column field="his_refcol3" title="HIS_RefCol3" width="80"></vxe-table-column>
                            <vxe-table-column field="uncharge_reason" title="退费原因" width="120"></vxe-table-column>
                        </vxe-table>
                    </div>
                </div>
            </div>
        </div>
        <el-dialog v-el-drag-dialog :show-close="false" :append-to-body="true" :close-on-click-modal="false" title="计价/退费结果一览" :visible.sync="dialogVisible" v-if="dialogVisible" width="740px" >
            <table style="width: 100%">
                <tr>
                    <td colspan="4">
                        <div style="width: 100%">
                            <vxe-table show-overflow ref="ChargeResultTable" height="400px" :loading="chargeloading" :sort-config="{ trigger: 'cell' }" :select-config="{ checkAll: true }" :keyboard-config="keyLocalConfig" :data.sync="ChargeResultData" :cell-class-name="ChargeClass" border size="mini" show-header-overflow highlight-current-row highlight-hover-row resizable stripe header-align="center">
                                <vxe-table-column type="index" title="NO." width="40" align="center"></vxe-table-column>
                                <vxe-table-column field="pat_no" title="病历号" width="80"></vxe-table-column>
                                <vxe-table-column field="pat_name" title="姓名" width="80"></vxe-table-column>
                                <vxe-table-column field="barcode" title="条码号" width="100"></vxe-table-column>
                                <vxe-table-column field="req_itemname" title="申请项目名称" width="160"></vxe-table-column>
                                <vxe-table-column field="req_itemcode" title="申请项目代码" width="100"></vxe-table-column>
                                <vxe-table-column field="cp_msg" title="计价\退费结果消息" width="160"></vxe-table-column>
                            </vxe-table>
                        </div>
                    </td>
                </tr>
            </table>
            <div style="text-align: right">
                <span style="color:green;margin-right:10px;">成功：{{successcount}}</span>
                <span style="color:red;margin-right:10px;">失败：{{failcount}}</span>
                <span style="color:blue;margin-right:10px;">已完成/总数：{{finishcount}}/{{totalcount}}</span>
                <el-button @click="ExitCharge()">退 出</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import XEUtils from "xe-utils"; //引入工具类
export default {
    name: "LIS253",
    data() {
        return {
            searchParam: {
                rptunitid: 29,
                datetime1: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
                datetime2: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
                pat_typecode: "3",
                charge_flag: "1",
            },
            tableList: [],
            tableLoading: false,
            keyLocalConfig: {
                isArrow: true,
                isDel: true,
                isTab: true
            },
            isloginsuccessflag: false,
            CurrentRows: [],
            barcodeArr: [],
            barcodeindex: 0,
            dialogVisible: false,
            ChargeResultData: [],
            chargeloading: false,
            successcount: 0,
            failcount: 0,
            finishcount: 0,
            totalcount: 0,
            rptunitname: "",
            pattypename: "",
        }
    },
    methods: {
        queryRequncharge() {
            this.tableLoading = true;
            this.$tool.request({
                    url: "LIS/LIS2/LIS253",
                    method: "post",
                    data: {
                        action: "QueryReqUncharge",
                        ...this.searchParam
                    }
                })
                .then((res) => {
                    this.tableLoading = false;
                    this.tableList = res.data;
                })
                .catch((e) => {
                    this.tableLoading = false;
                    this.$message.error(e);
                });
        },
        formatChargeflag ({row, column, cellValue}) {
            if (!column) return "";
            if (column.property == "charge_flag") {
                if (cellValue == "1") {
                    return "已计价";
                } else if (cellValue == "2") {
                    return "待计价";
                } else if (cellValue == "8") {
                    return "待退费";
                } else if (cellValue == "9") {
                    return "已退费";
                } else if (cellValue == "0") {
                    return "未计价";
                } else {
                    return cellValue;
                }
            }
        },
        BeforeBillingAndRefund(type) {
            //获取已选择的行数据
            this.CurrentRows = this.$refs.xTable.getSelectRecords();
            if (this.CurrentRows.length == 0) {
                this.$message.warning(type == 1 ? "请选择需要计价的数据" : "请选择需要退费的数据");
                return;
            }
            if(!this.isloginsuccessflag || type == 2) {
                this.$rm_Login({
                    type: "用户登录验证",
                    btn: {
                        confirmBtnText: "确定",
                        cancelBtnText: "取消",
                    },
                })
                    .then((res) => {
                        if (res.flag == "1") {
                            this.isloginsuccessflag = true;
                            //登录成功
                            this.reqBillingAndreqRefund(res.userid, type);
                        } else {
                            //登录失败
                            this.$message.error("用户登录失败");
                        }
                    })
                    .catch((e) => {});
            }
            else{
                this.reqBillingAndreqRefund(this.$tool.getSession(this.$tool.loginuserno), type);
            }
        },
        reqBillingAndreqRefund(userno, type) {
            console.log(this.CurrentRows);
            //根据条码号进行分组，来进行计费/退费操作
            this.barcodeArr = [];
            for (let i = 0; i < this.CurrentRows.length; i++) {
                if (this.barcodeArr.indexOf(this.CurrentRows[i].barcode) == -1) {
                    this.barcodeArr.push(this.CurrentRows[i].barcode);
                }
            }
            console.log(this.barcodeArr);
            if(this.barcodeArr.length > 0){
                this.dialogVisible = true;
                this.barcodeindex = 0;
                this.finishcount = 0;
                this.successcount = 0;
                this.failcount = 0;
                this.totalcount = this.CurrentRows.length;
                this.ChargeResultData = [];
                this.reqBillingAndreqRefundExec(this.barcodeArr[this.barcodeindex], type, userno);
            }
        },
        reqBillingAndreqRefundExec(barcode, type, userno) {
            let selectrows = this.CurrentRows.filter(item => item.barcode == barcode);
            this.$tool.request({
                    url: "LIS/LIS2/LIS253",
                    method: "post",
                    data: {
                        action: "reqbillingorrefund",
                        barcode: barcode,
                        selectstr: selectrows,
                        userno: userno,
                        type: type,
                    }
                })
                .then((res) => {
                    let resmsg = "";
                    if(res.flag == 1) 
                    {
                        this.successcount+=selectrows.length;
                        resmsg = type == 1 ? "计价成功！": "退费成功！";
                    }
                    else 
                    {
                        this.failcount+=selectrows.length;
                        resmsg = type == 1 ? "计价失败："+res.msg : "退费失败："+res.msg;
                    }
                    let resultdata = XEUtils.clone(selectrows);
                    resultdata.forEach(item => {
                        item.cp_msg = resmsg;
                    });
                    this.ChargeResultData.push(...resultdata);

                    if(++this.barcodeindex < this.barcodeArr.length){
                        this.reqBillingAndreqRefundExec(this.barcodeArr[this.barcodeindex], type, userno);
                    }else{
                        if(this.successcount > 0){
                            this.$message.success(type == 1 ? "全部计价结束！": "全部退费结束！");
                        }
                        else{
                            this.$message.error(type == 1 ? "全部计价失败！": "全部退费失败！");
                        }
                        this.barcodeindex = 0;
                    }
                    this.finishcount+=selectrows.length;
                })
                .catch((e) => {
                    this.$message.error(e);
                });
        },
        ExitCharge() {
            this.dialogVisible = false;
            this.queryRequncharge();
        },
        ChargeClass({ row, column }) {
            if (column.property == "cp_msg") {
                if (row.cp_msg != "" && row.cp_msg != "计价成功！" && row.cp_msg != "退费成功！") {
                    return "fail";
                }
                if (row.cp_msg != "" && (row.cp_msg == "计价成功！" || row.cp_msg == "退费成功！")) {
                    return "success";
                }
            }
        },
        rptunitchange(code, name) {
            this.searchParam.rptunitid = code;
            this.rptunitname = name;
            this.queryRequncharge();
        },
        pattypechange(code, name) {
            this.searchParam.pat_typecode = code;
            this.pattypename = name;
            this.queryRequncharge();
        },
        exportExcel() {
            let tableColumn = this.$refs.xTable.getColumns();
            let extableColumn = [];
            tableColumn.forEach((item) => {
                extableColumn.push({ title: item.title, field: item.property });
            });
            let filtertxt = this.getExportFilter();
            this.$pub.exportTableExcel(this.tableList, extableColumn, "LIS253 申请单退费.xlsx", filtertxt);
        },
        // 获取当前导出文件的所有查询条件
        getExportFilter() {
            let filtertxt = "标本日期:" + XEUtils.toDateString(this.searchParam.datetime1, "yyyy-MM-dd") + "至" + XEUtils.toDateString(this.searchParam.datetime2, "yyyy-MM-dd") + "; ";
            if(this.searchParam.rptunitid) filtertxt += "报告单元:" + this.rptunitname + "; ";
            if(this.searchParam.pat_no) filtertxt += "病历号:" + this.searchParam.pat_no + "; ";
            if(this.searchParam.pat_name) filtertxt += "姓名:" + this.searchParam.pat_name + "; ";
            if(this.searchParam.req_bedno) filtertxt += "床号:" + this.searchParam.req_bedno + "; ";
            filtertxt += "计价标志:" + this.getChargeName() + "; ";
            if(this.searchParam.barcode) filtertxt += "条码号:" + this.searchParam.barcode + "; ";
            if(this.searchParam.pat_typecode) filtertxt += "病人类型:" + this.pattypename + "; ";
            if(this.searchParam.uncharge_reason) filtertxt += "退费原因:" + this.searchParam.uncharge_reason + "; ";
            if(this.searchParam.rptunitidflag == "1") filtertxt += "无对应报告; ";
            return filtertxt;
        },
        getChargeName() {
            let name = "";
            switch(this.searchParam.charge_flag) {
                case "0":
                    name = "未计价";
                    break;
                case "1":
                    name = "已计价";
                    break;
                case "8":
                    name = "待退费";
                    break;
                case "9":
                    name = "已退费";
                    break;
                default:
                    name = "全部";
                    break;
            }
            return name;
        },
        clearSearchParam() {
            this.searchParam = {
                rptunitid: 29,
                datetime1: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
                datetime2: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
                pat_typecode: "3",
                charge_flag: "1",
                rptunitidflag: "0",
            };
        }
    }
}
</script>

<style scoped>
table {
    font-size: 12px;
    border-collapse: separate;
    border-spacing: 5px;
    width: 100%;
}
.pagetop {
    left: 0;
    right: 0;
    height: 100px;
    /* display: -webkit-box; */
    -webkit-box-pack: center;
    -webkit-box-align: center;
    border: solid 1px #ccc;
    position: absolute;
    z-index: 99;
    overflow-x: auto;
    overflow-x: overlay;
    white-space: nowrap;
}
::-webkit-scrollbar {
    width: 0px;
    height: 5px;
    border: 4px solid white;
}
::-webkit-scrollbar-thumb {  
    background-color: #c1c1c1;  
} 
::-webkit-scrollbar-track {
    background-color: #f1f1f1;
}
/deep/ .pagetop .el-input__icon {
    width: 16px !important;
}
/deep/ .pagetop .el-input--prefix .el-input__inner {
    padding-left: 22px !important;
}
/deep/ .pagetop .el-input--suffix .el-input__inner {
    padding-right: 22px !important;
}
/deep/ .search-min .el-input--small .el-input__inner {
    text-align: right !important;
}
/deep/.fail {
    color: red;
}
/deep/.success {
    color: green;
}
.top-row {
    display: block;
    width: 100%;
    font-size: 12px;
    margin-top: 5px;
}
.top-row span {
    display: inline-block;
}
.top-row-title {
    margin:  0 5px;
    width: 50px;
    text-align: right;
}
.top-row-item {
    width: 118px;
}
.top-row-item1 {
    width: 130px;
}
.top-row-item2 {
    width: 239px;
}
.top-button {
    text-align: right;
    padding: 5px 5px 0 5px;
}

.pagecenter {
    position: absolute;
    top: 105px;
    width: 100%;
    left: 0;
    bottom: 0px;
    overflow: auto;
}
/deep/ .pagecenter .vxe-table .vxe-header--column {
    line-height: 18px !important;
}
</style>